package com.lizemin.springaspect.filter;

import com.lizemin.springaspect.interceptor.RepeatableRequestWrapper;
import com.lizemin.springaspect.interceptor.RepeatableResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lzm
 * @date 2025/3/31 20:37
 * @description
 */
@WebFilter("/*")
@Slf4j
public class ContentCacheFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 替换原始请求对象
        RepeatableRequestWrapper requestWrapper = new RepeatableRequestWrapper(request);
        RepeatableResponseWrapper responseWrapper = new RepeatableResponseWrapper(response);
        filterChain.doFilter(requestWrapper, responseWrapper);
    }

}
